package
{
	/**
	 * 组件编辑器
	 * @author kings
	 * @E-mail: qywangweiqiang@126.com
	 * 2012-11-11下午05:02:30
	 * */
	import fl.controls.CheckBox;
	import fl.controls.ScrollBarDirection;
	import fl.controls.ScrollPolicy;
	import fl.controls.TileList;
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoaderDataFormat;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	
	import kingsAsLib.displays.Controls.KLabel;
	import kingsAsLib.displays.Controls.KScrollBarContent;
	import kingsAsLib.displays.Controls.Klist;
	import kingsAsLib.displays.ReferenceWin;
	import kingsAsLib.nets.KUrlLoader;
	import kingsAsLib.systems.KSwfDataPool;
	import kingsAsLib.air.KFileDirectory;
	import kingsAsLib.air.KwriterFile;
	
	import utils.ClassNameImport;

	[SWF(width="1000", height="600")]
	public class UIEdits extends Sprite
	{
		private var uiPanStages:Stage;
		private var classList:TileList;

		private var uiPan:Sprite;

		private var uiPanList:Vector.<DisplayObject>=new Vector.<DisplayObject>;
		//移动框
		private var uiMoveBox:ReferenceWin=new ReferenceWin();

		public function UIEdits()
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event):void
		{
			var _clsNameImport:ClassNameImport=new ClassNameImport();

			initUI();
			loadFileList();
		}

		private function initUI():void
		{

			classList=new TileList();
			classList.setSize(150, 100);
			classList.columnWidth=200;
			classList.rowHeight=24;
			classList.columnCount=1;
			classList.rowCount=20;
			classList.direction=ScrollBarDirection.VERTICAL;
			classList.verticalScrollPolicy=ScrollPolicy.OFF
			classList.move(10, 10);
			addChild(classList);

			uiPan=new Sprite();
			addChild(uiPan);
			uiPanStages=uiPan.stage;

			uiPan.x=classList.x + classList.width + 10;
			uiPan.y=classList.y;
			uiPan.graphics.beginFill(0, 0.3);
			uiPan.graphics.drawRect(0, 0, 600, 600);
			uiPan.graphics.endFill();

//			uiMoveBox.start(uiPanStages, 100, 100,0xff0000,1,1,0x000000,1);
		}

		private function loadFileList():void
		{
			// 
			var kurl:KUrlLoader=new KUrlLoader();
			kurl.loaderStars("flieList.txt", loadCompleteFun, null, URLLoaderDataFormat.TEXT);
		}

		private function loadCompleteFun(obj:String):void
		{
			var classArr:Vector.<String>=new Vector.<String>;
			var arr:Array=obj.split(",");
			var myPattern:RegExp=/\//gi;
			for (var i:int=0; i < arr.length; i++)
			{
				var str:String=arr[i];
				var temp:String=str.replace(myPattern, ".");
				var temp2:String=temp.substr(0, temp.length - 3);
				classArr.push(temp2);
			}

			initClassList(classArr);
			//			KSwfDataPool.getInstance().load("fl.swf", loadfun);
		}

		private function loadfun():void
		{
			// 
		}

		private function initClassList(classArr:Vector.<String>):void
		{
			var sp:Sprite=new Sprite();
			var dp:DataProvider=new DataProvider();

			for (var i:int=0; i < classArr.length; i++)
			{
				dp.addItem({label:classArr[i]});
			}


			classList.dataProvider=dp;

			classList.addEventListener(fl.events.ListEvent.ITEM_CLICK, classItemClickHandle);
		}

		protected function classItemClickHandle(e:ListEvent):void
		{

			var item:Object=e.item;
			var className:String=item.label;

			if (className == null)
			{
				return;
			}

			var cls:Class=getClass(className);
			if (cls)
			{
				if (new cls as DisplayObject)
				{
					showUI(new cls as DisplayObject);
				}
			}
		}

		private function showUI(obj:DisplayObject):void
		{
			// 
			uiPanList.push(obj);

			uiPan.addChild(obj);
			initAddListener();
		}

		private function initAddListener():void
		{
			// 
			for each (var i:DisplayObject in uiPanList)
			{
				i.addEventListener(MouseEvent.MOUSE_DOWN, UIMouseDownHandle);
			
			}

		}

		protected function UIMouseUPHandle(dis:DisplayObject):void
		{
				dis.x=uiMoveBox.targetXY().x;
				dis.y=uiMoveBox.targetXY().y;

				uiPan.removeChild(uiMoveBox);
				uiMoveBox.destory();
		}

		protected function UIMouseDownHandle(event:Event):void
		{
			var dis:DisplayObject=event.target as DisplayObject;
			if (dis)
			{
				if(!(dis is Sprite)){
					dis=dis.parent;
				}
				uiMoveBox.start(uiPan, dis,UIMouseUPHandle);
				uiMoveBox.x=dis.x;
				uiMoveBox.y=dis.y;
				uiPan.addChild(uiMoveBox);

			}

		}

		public function getClass(name:String):Class
		{
			try
			{
				var ClassReference:Class=getDefinitionByName(name) as Class;
			}
			catch (e:Error)
			{
				trace("getClass " + name + "error" + e.message);
				return null;
			}
			return ClassReference;
		}

	}

	//end class
}
